﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using DAL;
using DTO;
using System.ComponentModel;

namespace BLL
{
    public partial class FilterFunc : ExcelFunc
    {
        private DataTable _dtDupList;
        private List<DataTable> _dtDeDupList = new List<DataTable>();
        public DataTable DupList
        {
            get { return _dtDupList; }
        }
        public List<DataTable> DeDupList
        {
            get { return _dtDeDupList; }
        }

        public void FilterDupList(DataTable table)
        {
            _dtDupList = table.Clone();
            List<IGrouping<object, DataRow>> duplicates = new List<IGrouping<object, DataRow>>();

            for (int i = 6; i < 9; i++)
            {
                duplicates = table.AsEnumerable().GroupBy(r => r[i]).Where(gr => gr.Count() > 1 && gr.Key.ToString() != "").ToList();
                if (duplicates != null && duplicates.Count > 0)
                {
                    foreach (var row in duplicates)
                    {
                        foreach (DataRow dup in row.CopyToDataTable().Rows)
                        {
                            _dtDupList.Rows.Add(dup.ItemArray);
                        }
                    }
                }
            }
            if (_dtDupList.Rows.Count > 0)
            {
                var tmp = _dtDupList.AsEnumerable().Distinct(DataRowComparer.Default);
                _dtDupList = tmp.CopyToDataTable();
            }

        }

        public void FilterDeDupList(DataTable table)
        {
            CustomerService cusDA = new CustomerService();
            ArrayList duptmpExcel = new ArrayList();
            List<CustomerEntity> duptmpDtbase = new List<CustomerEntity>();            

            foreach (DataRow row in table.Rows)
            {
                var tmp = cusDA.LoadCustomerByCellPhone(row["CellPhoneNumber"].ToString());
                if (tmp != null && tmp.EDD != null)
                {
                    tmp.EDDS = HelperClass.GetEDDS(tmp.EDD);
                    tmp.EDDM = HelperClass.GetEDDM((DateTime)tmp.EDD);
                    tmp.EDDW = HelperClass.GetEDDWeek((DateTime)tmp.EDD) + "W";
                }
                if (tmp != null)
                {
                    if (tmp.EDD.HasValue)
                    {
                        if (row["CellPhoneNumber"].ToString().Equals(tmp.CellPhoneNumber) &&
                            !string.IsNullOrWhiteSpace(row["CellPhoneNumber"].ToString()) &&
                            (280 + (ServerDateTime.SystemDateTime - (DateTime)tmp.EDD).Days) < 300)
                        {
                            duptmpExcel.Add(row);
                            duptmpDtbase.Add(tmp);
                        }
                    }
                }
            }
            foreach (DataRow row in table.Rows)
            {
                var tmp = cusDA.LoadCustomerByHomePhone(row["HomePhoneNumber"].ToString());
                if (tmp != null && tmp.EDD != null)
                {
                    tmp.EDDS = HelperClass.GetEDDS(tmp.EDD);
                    tmp.EDDM = HelperClass.GetEDDM((DateTime)tmp.EDD);
                    tmp.EDDW = HelperClass.GetEDDWeek((DateTime)tmp.EDD) + "W";
                }
                if (tmp != null)
                {
                    if (tmp.EDD.HasValue)
                    {
                        if (row["HomePhoneNumber"].ToString().Equals(tmp.HomePhoneNumber) &&
                            !string.IsNullOrWhiteSpace(row["HomePhoneNumber"].ToString()) &&
                            (280 + (ServerDateTime.SystemDateTime - (DateTime)tmp.EDD).Days) < 300)
                        {
                            duptmpExcel.Add(row);
                            duptmpDtbase.Add(tmp);
                        }
                    }
                }
            }
            foreach (DataRow row in table.Rows)
            {
                var tmp = cusDA.LoadCustomerByOfficePhone(row["OfficePhoneNumber"].ToString());
                if (tmp != null && tmp.EDD != null)
                {
                    tmp.EDDS = HelperClass.GetEDDS(tmp.EDD);
                    tmp.EDDM = HelperClass.GetEDDM((DateTime)tmp.EDD);
                    tmp.EDDW = HelperClass.GetEDDWeek((DateTime)tmp.EDD) + "W";
                }
                if (tmp != null)
                {
                    if (tmp.EDD.HasValue)
                    {
                        if (row["OfficePhoneNumber"].ToString().Equals(tmp.OfficePhoneNumber) &&
                            !string.IsNullOrWhiteSpace(row["OfficePhoneNumber"].ToString()) &&
                            (280 + (ServerDateTime.SystemDateTime - (DateTime)tmp.EDD).Days) < 300)
                        {
                            duptmpExcel.Add(row);
                            duptmpDtbase.Add(tmp);
                        }
                    }
                }
            }            
            //add table dup excel
            _dtDeDupList.Add(table.Clone());
            //add table dup database
            _dtDeDupList.Add(HelperClass.ToDataTable(duptmpDtbase));            
            foreach (DataRow row in duptmpExcel)
            {
                _dtDeDupList[0].Rows.Add(row.ItemArray);
            }
            if (_dtDeDupList[0].Rows.Count > 0 && _dtDeDupList[1].Rows.Count > 0)
            {
                _dtDeDupList[0] = _dtDeDupList[0].AsEnumerable().Distinct(DataRowComparer.Default).CopyToDataTable();
                _dtDeDupList[1] = _dtDeDupList[1].AsEnumerable().Distinct(DataRowComparer.Default).CopyToDataTable();
            }
        }

        public void DeleteDup(List<DataRow> ldRow)
        {
            foreach (DataRow dRow in ldRow)
            {
                var delete = dtRaw.AsEnumerable().Where(r => r.ItemArray.SequenceEqual(dRow.ItemArray)).SingleOrDefault();
                dtRaw.Rows.Remove(delete);
            }            
            LoadDataToUI(dtRaw);
        }

        public void DeleteDedup(DataTable table)
        {
            foreach (DataRow row in table.Rows)
            {
                var delete = dtRaw.AsEnumerable().Where(r => r.ItemArray.SequenceEqual(row.ItemArray)).SingleOrDefault();
                if (delete != null)
                {
                    dtRaw.Rows.Remove(delete);
                    LoadDataToUI(dtRaw);
                }
            }
        }

    }
}
